/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pig.mt.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pig.mt.common.api.handler.JSONArrayTypeHandler;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 用户表
 *
 * @author JLM
 * @date 2021-03-05 11:45:09
 */
@Data
@TableName("wx_user")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "用户表")
public class WxUser extends Model<WxUser> {
private static final long serialVersionUID = 1L;

    /**
     * 微信openid
     */
    @TableId(type = IdType.ASSIGN_UUID)
    @ApiModelProperty(value="微信openid")
    private String openid;
    /**
     * appid
     */
    @ApiModelProperty(value="appid")
    private String appid;
    /**
     * 手机号
     */
    @ApiModelProperty(value="手机号")
    private String phone;
    /**
     * 昵称
     */
    @ApiModelProperty(value="昵称")
    private String nickname;
    /**
     * 性别(0-未知、1-男、2-女)
     */
    @ApiModelProperty(value="性别(0-未知、1-男、2-女)")
    private Integer sex;
    /**
     * 城市
     */
    @ApiModelProperty(value="城市")
    private String city;
    /**
     * 省份
     */
    @ApiModelProperty(value="省份")
    private String province;
    /**
     * 头像
     */
    @ApiModelProperty(value="头像")
    private String headimgurl;
    /**
     * 订阅时间
     */
    @ApiModelProperty(value="订阅时间")
    private Date subscribeTime;
    /**
     * 是否关注
     */
    @ApiModelProperty(value="是否关注")
    private Boolean subscribe;
    /**
     * unionid
     */
    @ApiModelProperty(value="unionid")
    private String unionid;
    /**
     * 备注
     */
    @ApiModelProperty(value="备注")
    private String remark;
    /**
     * 标签ID列表
     */
    @ApiModelProperty(value="标签ID列表")
	@TableField(typeHandler = JSONArrayTypeHandler.class)
	private JSONArray tagidList;
    /**
     * 关注场景
     */
    @ApiModelProperty(value="关注场景")
    private String subscribeScene;
    /**
     * 扫码场景值
     */
    @ApiModelProperty(value="扫码场景值")
    private String qrSceneStr;

	public WxUser() {
	}

	public WxUser(String openid) {
		this.openid = openid;
	}

	public WxUser(WxMpUser wxMpUser, String appid) {
		this.openid = wxMpUser.getOpenId();
		this.appid = appid;
		this.subscribe=wxMpUser.getSubscribe();
		if(wxMpUser.getSubscribe()){
			this.nickname = wxMpUser.getNickname();
			this.sex = wxMpUser.getSex();
			this.city = wxMpUser.getCity();
			this.province = wxMpUser.getProvince();
			this.headimgurl = wxMpUser.getHeadImgUrl();
			this.subscribeTime = new Date(wxMpUser.getSubscribeTime()*1000);
			this.unionid=wxMpUser.getUnionId();
			this.remark=wxMpUser.getRemark();
			this.tagidList=JSONArray.parseArray(JSONObject.toJSONString(wxMpUser.getTagIds()));
			this.subscribeScene=wxMpUser.getSubscribeScene();
			String qrScene =  wxMpUser.getQrScene();
			this.qrSceneStr= StringUtils.isEmpty(qrScene) ? wxMpUser.getQrSceneStr() : qrScene;
		}
	}
}
